//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using System.Text;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;
using System.ComponentModel;
using Tathata.Common;

namespace Tathata.Web.Modules.Account
{
    public partial class CustomerInfoControl : BaseNopUserControl
    {
        private void ApplyLocalization()
        {
            rbGenderM.Text = GetLocaleResourceString("Account.GenderMale");
            rbGenderF.Text = GetLocaleResourceString("Account.GenderFemale");
            rfvFirstName.ErrorMessage = GetLocaleResourceString("Account.FirstNameIsRequired");
            rfvFirstName.ToolTip = GetLocaleResourceString("Account.FirstNameIsRequired");
            rfvLastName.ErrorMessage = GetLocaleResourceString("Account.LastNameIsRequired");
            rfvLastName.ToolTip = GetLocaleResourceString("Account.LastNameIsRequired");
            rfvStreetAddress.ErrorMessage = GetLocaleResourceString("Account.StreetAddressIsRequired");
            rfvStreetAddress.ToolTip = GetLocaleResourceString("Account.StreetAddressIsRequired");
            rfvZipPostalCode.ErrorMessage = GetLocaleResourceString("Account.ZipPostalCodeIsRequired");
            rfvZipPostalCode.ToolTip = GetLocaleResourceString("Account.ZipPostalCodeIsRequired");
            rfvCity.ErrorMessage = GetLocaleResourceString("Account.CityIsRequired");
            rfvCity.ToolTip = GetLocaleResourceString("Account.CityIsRequired");
            rfvPhoneNumber.ErrorMessage = GetLocaleResourceString("Account.PhoneNumberIsRequired");
            rfvPhoneNumber.ToolTip = GetLocaleResourceString("Account.PhoneNumberIsRequired");
            btnSaveCustomerInfo.Text = GetLocaleResourceString("Common.Save");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ApplyLocalization();

            if (NopContext.Current.User == null)
                Response.Redirect("~/Login.aspx");

            if (!Page.IsPostBack)
            {
                FillCountryDropDowns();
                FillStateProvinceDropDowns();
                BindCustomerAttributes();
            }
        }

        private void BindCustomerAttributes()
        {
            Customer customer = NopContext.Current.User;
            CustomerAttributeCollection customerAttributes = customer.CustomerAttributes;

            CustomerAttribute genderAttr = customerAttributes.FindAttribute("Gender", customer.CustomerID);
            if (genderAttr != null)
            {
                if (genderAttr.Value.ToLower() == "m")
                    rbGenderM.Checked = true;
                else
                    rbGenderF.Checked = true;
            }
            else
                rbGenderM.Checked = true;

            CustomerAttribute firstNameAttr = customerAttributes.FindAttribute("FirstName", customer.CustomerID);
            if (firstNameAttr != null)
                txtFirstName.Text = firstNameAttr.Value;

            CustomerAttribute lastNameAttr = customerAttributes.FindAttribute("LastName", customer.CustomerID);
            if (lastNameAttr != null)
                txtLastName.Text = lastNameAttr.Value;

            CustomerAttribute dateOfBirthAttr = customerAttributes.FindAttribute("DateOfBirth", customer.CustomerID);
            if (dateOfBirthAttr != null)
            {
                try
                {
                    txtDateOfBirth.Text = XmlHelper.DeserializeDateTime(dateOfBirthAttr.Value).ToString("d");
                }
                catch
                {
                }
            }
            CustomerAttribute companyAttr = customerAttributes.FindAttribute("Company", customer.CustomerID);
            if (companyAttr != null)
                txtCompany.Text = companyAttr.Value;

            CustomerAttribute streetAddressAttr = customerAttributes.FindAttribute("StreetAddress", customer.CustomerID);
            if (streetAddressAttr != null)
                txtStreetAddress.Text = streetAddressAttr.Value;

            CustomerAttribute streetAddress2Attr = customerAttributes.FindAttribute("StreetAddress2", customer.CustomerID);
            if (streetAddress2Attr != null)
                txtStreetAddress2.Text = streetAddress2Attr.Value;

            CustomerAttribute zipPostalCodeAttr = customerAttributes.FindAttribute("ZipPostalCode", customer.CustomerID);
            if (zipPostalCodeAttr != null)
                txtZipPostalCode.Text = zipPostalCodeAttr.Value;

            CustomerAttribute cityAttr = customerAttributes.FindAttribute("City", customer.CustomerID);
            if (cityAttr != null)
                txtCity.Text = cityAttr.Value;

            CustomerAttribute phoneNumberAttr = customerAttributes.FindAttribute("PhoneNumber", customer.CustomerID);
            if (phoneNumberAttr != null)
                txtPhoneNumber.Text = phoneNumberAttr.Value;

            CustomerAttribute faxNumberAttr = customerAttributes.FindAttribute("FaxNumber", customer.CustomerID);
            if (faxNumberAttr != null)
                txtFaxNumber.Text = faxNumberAttr.Value;

            CustomerAttribute countryIDAttr = customerAttributes.FindAttribute("CountryID", customer.CustomerID);
            if (countryIDAttr != null)
                CommonHelper.SelectListItem(ddlCountry, countryIDAttr.Value);

            FillStateProvinceDropDowns();

            CustomerAttribute stateProvinceIDAttr = customerAttributes.FindAttribute("StateProvinceID", customer.CustomerID);
            if (stateProvinceIDAttr != null)
                CommonHelper.SelectListItem(ddlStateProvince, stateProvinceIDAttr.Value);
        }

        protected void btnSaveCustomerInfo_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Customer customer = NopContext.Current.User;
                CustomerAttributeCollection customerAttributes = customer.CustomerAttributes;

                CustomerAttribute genderAttr = customerAttributes.FindAttribute("Gender", customer.CustomerID);
                {
                    string gender = string.Empty;
                    if (rbGenderM.Checked)
                        gender = "M";
                    else
                        gender = "F";

                    if (genderAttr != null)
                        genderAttr = CustomerAttributeManager.UpdateCustomerAttribute(genderAttr.CustomerAttributeID, genderAttr.CustomerID, "Gender", gender);
                    else
                        genderAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "Gender", gender);
                }

                CustomerAttribute firstNameAttr = customerAttributes.FindAttribute("FirstName", customer.CustomerID);
                if (firstNameAttr != null)
                    firstNameAttr = CustomerAttributeManager.UpdateCustomerAttribute(firstNameAttr.CustomerAttributeID, firstNameAttr.CustomerID, "FirstName", txtFirstName.Text);
                else
                    firstNameAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "FirstName", txtFirstName.Text);

                CustomerAttribute lastNameAttr = customerAttributes.FindAttribute("LastName", customer.CustomerID);
                if (lastNameAttr != null)
                    lastNameAttr = CustomerAttributeManager.UpdateCustomerAttribute(lastNameAttr.CustomerAttributeID, lastNameAttr.CustomerID, "LastName", txtLastName.Text);
                else
                    lastNameAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "LastName", txtLastName.Text);

                CustomerAttribute dateOfBirthAttr = customerAttributes.FindAttribute("DateOfBirth", customer.CustomerID);
                try
                {
                    DateTime dateOfBirth = DateTime.Parse(txtDateOfBirth.Text);
                    if (dateOfBirthAttr != null)
                        dateOfBirthAttr = CustomerAttributeManager.UpdateCustomerAttribute(dateOfBirthAttr.CustomerAttributeID, dateOfBirthAttr.CustomerID, "DateOfBirth", XmlHelper.SerializeDateTime(dateOfBirth));
                    else
                        dateOfBirthAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "DateOfBirth", XmlHelper.SerializeDateTime(dateOfBirth));
                }
                catch
                {
                }
                CustomerAttribute companyAttr = customerAttributes.FindAttribute("Company", customer.CustomerID);
                if (companyAttr != null)
                    companyAttr = CustomerAttributeManager.UpdateCustomerAttribute(companyAttr.CustomerAttributeID, companyAttr.CustomerID, "Company", txtCompany.Text);
                else
                    companyAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "Company", txtCompany.Text);

                CustomerAttribute streetAddressAttr = customerAttributes.FindAttribute("StreetAddress", customer.CustomerID);
                if (streetAddressAttr != null)
                    streetAddressAttr = CustomerAttributeManager.UpdateCustomerAttribute(streetAddressAttr.CustomerAttributeID, streetAddressAttr.CustomerID, "StreetAddress", txtStreetAddress.Text);
                else
                    streetAddressAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "StreetAddress", txtStreetAddress.Text);

                CustomerAttribute streetAddress2Attr = customerAttributes.FindAttribute("StreetAddress2", customer.CustomerID);
                if (streetAddress2Attr != null)
                    streetAddress2Attr = CustomerAttributeManager.UpdateCustomerAttribute(streetAddress2Attr.CustomerAttributeID, streetAddress2Attr.CustomerID, "StreetAddress2", txtStreetAddress2.Text);
                else
                    streetAddress2Attr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "StreetAddress2", txtStreetAddress2.Text);

                CustomerAttribute zipPostalCodeAttr = customerAttributes.FindAttribute("ZipPostalCode", customer.CustomerID);
                if (zipPostalCodeAttr != null)
                    zipPostalCodeAttr = CustomerAttributeManager.UpdateCustomerAttribute(zipPostalCodeAttr.CustomerAttributeID, zipPostalCodeAttr.CustomerID, "ZipPostalCode", txtZipPostalCode.Text);
                else
                    zipPostalCodeAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "ZipPostalCode", txtZipPostalCode.Text);

                CustomerAttribute cityAttr = customerAttributes.FindAttribute("City", customer.CustomerID);
                if (cityAttr != null)
                    cityAttr = CustomerAttributeManager.UpdateCustomerAttribute(cityAttr.CustomerAttributeID, cityAttr.CustomerID, "City", txtCity.Text);
                else
                    cityAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "City", txtCity.Text);

                CustomerAttribute phoneNumberAttr = customerAttributes.FindAttribute("PhoneNumber", customer.CustomerID);
                if (phoneNumberAttr != null)
                    phoneNumberAttr = CustomerAttributeManager.UpdateCustomerAttribute(phoneNumberAttr.CustomerAttributeID, phoneNumberAttr.CustomerID, "PhoneNumber", txtPhoneNumber.Text);
                else
                    phoneNumberAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "PhoneNumber", txtPhoneNumber.Text);

                CustomerAttribute faxNumberAttr = customerAttributes.FindAttribute("FaxNumber", customer.CustomerID);
                if (faxNumberAttr != null)
                    faxNumberAttr = CustomerAttributeManager.UpdateCustomerAttribute(faxNumberAttr.CustomerAttributeID, faxNumberAttr.CustomerID, "FaxNumber", txtFaxNumber.Text);
                else
                    faxNumberAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "FaxNumber", txtFaxNumber.Text);

                CustomerAttribute countryIDAttr = customerAttributes.FindAttribute("CountryID", customer.CustomerID);
                if (countryIDAttr != null)
                    countryIDAttr = CustomerAttributeManager.UpdateCustomerAttribute(countryIDAttr.CustomerAttributeID, countryIDAttr.CustomerID, "CountryID", ddlCountry.SelectedItem.Value);
                else
                    countryIDAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "CountryID", ddlCountry.SelectedItem.Value);

                CustomerAttribute stateProvinceIDAttr = customerAttributes.FindAttribute("StateProvinceID", customer.CustomerID);
                if (stateProvinceIDAttr != null)
                    stateProvinceIDAttr = CustomerAttributeManager.UpdateCustomerAttribute(stateProvinceIDAttr.CustomerAttributeID, stateProvinceIDAttr.CustomerID, "StateProvinceID", ddlStateProvince.SelectedItem.Value);
                else
                    stateProvinceIDAttr = CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "StateProvinceID", ddlStateProvince.SelectedItem.Value);
            }
        }

        private void FillCountryDropDowns()
        {
            ddlCountry.Items.Clear();
            CountryCollection countryCollection = CountryManager.GetAllCountriesForRegistration();
            foreach (Country country in countryCollection)
            {
                ListItem ddlCountryItem2 = new ListItem(country.Name, country.CountryID.ToString());
                ddlCountry.Items.Add(ddlCountryItem2);
            }
        }

        private void FillStateProvinceDropDowns()
        {
            ddlStateProvince.Items.Clear();
            int countryID = 0;
            if (ddlCountry.SelectedItem != null)
                countryID = int.Parse(ddlCountry.SelectedItem.Value);

            StateProvinceCollection stateProvinceCollection = StateProvinceManager.GetAllByCountryID(countryID);
            foreach (StateProvince stateProvince in stateProvinceCollection)
            {
                ListItem ddlStateProviceItem2 = new ListItem(stateProvince.Name, stateProvince.StateProvinceID.ToString());
                ddlStateProvince.Items.Add(ddlStateProviceItem2);
            }
            if (stateProvinceCollection.Count == 0)
            {
                ListItem ddlStateProvinceItem = new ListItem(GetLocaleResourceString("Common.StateProvinceNonUS"), "0");
                ddlStateProvince.Items.Add(ddlStateProvinceItem);
            }
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillStateProvinceDropDowns();
        }
    }
}